package com.example.demo.LeetCode.arr;

public class MinSubArrayLen {
    public static void main(String[] args) {
         int[] nums = {2,3,1,2,4,3};
         int res = solution(nums,7);
         System.out.println(res);
    }

    public static int solution(int[] nums,int target){
        int result = Integer.MAX_VALUE;
        int sums = 0;
        int subLength = 0;
        int size = nums.length;
        int i = 0;//滑动窗口起始位置
        for (int j = 0 ; j < size ; j++){
            sums+= nums[j];
            while (sums >= target){
                subLength = j - i + 1;
                result = result < subLength ? result : subLength;
                //精髓，起始位置的移动
                sums-= nums[i++];
            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }
}
